# -*- coding: utf-8 -*-
# @Time    : 2018/8/15 13:27
# @Author  : 聂家利
# @Email   : 1073438012@qq.com
# @File    : centerUi.py
# @Software: PyCharm
import sys
import os

import pymel.core as pm
import maya.cmds as cmds
import maya.mel as mel

from Qt import QtCore
from Qt import QtGui
from Qt import QtWidgets
import maya.OpenMayaUI as omui
try:
    from shiboken2 import wrapInstance
except:
    from shiboken import wrapInstance
    
from Lib.message import Message
from Lib.center_ui_api import CenterUiApi

def _get_maya_main_windowsdow():
    pointer = omui.MQtUtil.mainWindow()
    return wrapInstance(long(pointer), QtWidgets.QWidget)

class center_ui(QtWidgets.QDialog):
    filename = ''
    now_scene_dir = ''

    def __init__(self):
        super(center_ui, self).__init__(_get_maya_main_windowsdow())
        self.ui_message = Message()
        self.init_ui()

    def init_ui(self):
        self.my_dir_lineedit = QtWidgets.QLineEdit()
        self.my_dir_lineedit.setPlaceholderText(u'我的文件夹目录')

        self.dest_dir_lineedit = QtWidgets.QLineEdit()
        self.dest_dir_lineedit.setPlaceholderText(u'目标地址')

        # self.file_name_lineedit = QtWidgets.QLineEdit(os.path.basename(pm.sceneName()))
        self.submit_server_btn = QtWidgets.QPushButton(u'文件名', self, clicked=self.submit_file_to_server)

        grid = QtWidgets.QGridLayout()
        grid.setSpacing(10)
        grid.addWidget(self.my_dir_lineedit, 1, 0)
        grid.addWidget(self.dest_dir_lineedit, 2, 0)
        # grid.addWidget(self.file_name_lineedit, 3, 0)
        grid.addWidget(self.submit_server_btn , 3, 0)

        # grid.addWidget(self.suffix_lable, 2, 1)
        self.setLayout(grid)
        self.setWindowTitle('submit_tool')
        self.resize(350, 300)

    def submit_file_to_server(self):
        my_dir_or_file = self.my_dir_lineedit.text().strip()
        dest_dir_or_file = self.dest_dir_lineedit.text().strip()
        source, dest = self.get_source_dest(my_dir_or_file, dest_dir_or_file)
        # 发送requests 请求 [发送内容：user_name, host_ip , 文件全路径，文件保存路径]
        Con = CenterUiApi(user_name='njl',  # 服务器记录日志专用 cg获取
                          file_path=r'{}'.format(source),
                          file_save_path=r'{}'.format(dest)  # cgtw 获取
         )
        # 发送数据到服务器
        r = Con.send_data()
        print('url', r.url)
        print('server return data', r.text)

    def get_source_dest(self, my_dir_or_file, dest_dir_or_file):
        # 返回源和目标的目录或者文件地址
        if os.path.isfile(my_dir_or_file):
            get_basename = os.path.basename(my_dir_or_file)
            get_dirname = self.get_dest_dir(dest_dir_or_file)
            dest = os.path.join(get_dirname, get_basename)
        elif os.path.isdir(my_dir_or_file):
            dest = self.get_dest_dir(dest_dir_or_file)
        else:
            dest = ''
            self.ui_message.warning(u'<div>源路径不存在</div>')
        return my_dir_or_file, dest

    def get_dest_dir(self, dest):
        # 返回目标的目录
        if '.' in dest:
            dest = os.path.dirname(dest)
        return dest


def main():
    global center_ui
    center_ui = center_ui()
    center_ui.show()


if __name__ == '__main__':
    main()